import os
import shutil
import re

with open('example.txt', 'r',encoding='UTF-8',errors = 'ignore') as file:
    lines = file.readlines()

# # 创建输出文件夹
output_folder = 'img_rename'
if not os.path.exists(output_folder):
    os.makedirs(output_folder)
# 分割每行内容并读取照片
for line in lines:
    parts = line.strip().split('---')
    if len(parts) == 2:
        photo_path = parts[0].strip()
        photo_content = parts[1].strip()
        pattern = re.compile('^[a-zA-Z0-9]+$')
        # 使用正则表达式匹配字符串
        if pattern.match(photo_content) and len(photo_content) == 4:
            # # 复制照片到输出文件夹
            photo_path = 'img/'+photo_path+".jpg"
            photo_name = photo_content+".jpg"
            if os.path.isfile(photo_path):
                shutil.copy(photo_path, os.path.join(output_folder, photo_name))
            else:
                print(photo_path)
        else:
            print('字符串不符合要求:',photo_content)
